#include <qevent.h>

#include "MouseDragWidget.h"

MouseDragWidget::MouseDragWidget(QWidget *parent): QWidget(parent),
		rightButton(false)
{
	
}

void MouseDragWidget::mousePressEvent(QMouseEvent *e)
{
	if (rightButton)
		emit mouseRightButtonPressed();
	else
		emit mouseLeftButtonPressed();
}

void MouseDragWidget::mouseReleaseEvent(QMouseEvent *e)
{
	if (rightButton)
		emit mouseRightButtonReleased();
	else
		emit mouseLeftButtonReleased();
}

void MouseDragWidget::mouseMoveEvent(QMouseEvent *e)
{
	const QPoint &p = e->pos();
	
	int x = p.x();
	int y = p.y();
	
	emit mouseMovedAbsolute(static_cast<short>((double)x / size().width() * USHRT_MAX), static_cast<short>((double)y / size().height() * USHRT_MAX));
	emit mouseMovedRelative(x - prevPoint.x(), y - prevPoint.y());
	
	prevPoint = p;
}

void MouseDragWidget::keyPressEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Shift)
		rightButton = true;
}

void MouseDragWidget::keyReleaseEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Shift)
		rightButton = false;
}
